﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Security.Principal;
using System.Threading;
using System.Web;
using Framework.Data.Exceptions;
using Framework.Data.DataBroker;

/// <summary>
/// Classe de gestion de la sécurité
/// </summary>
/// <remarks></remarks>
/// 
namespace Framework.Data.Security
{
    public class SecurityManager
    {
    	
    	#region "Members"
    	
    	protected static SecurityManager _instance = null;
        protected static object _lockObject = new object();
    	protected IDataBroker _dataBroker = null;
    	
    	#endregion
    	
    	#region "Constructor"
    	
    	private SecurityManager(){
    	}
    	
    	#endregion
    	
    	#region "Properties"
    	
    	public static SecurityManager Instance
        {
            get
            {
                lock (_lockObject)
                {
                    if ((_instance == null))
                    {
                        _instance = new SecurityManager();
                    }
                }
                return _instance;
            }
        }
    	
    	public static IDataBroker DataBroker
    	{
    		get{ return SecurityManager.Instance._dataBroker;}
    	}
    	
    	public static IPrincipal Principal{
    		get{ return Thread.CurrentPrincipal; }
    	}
    	
    	#endregion
        
        #region "Methods"
        
        public bool OpenConnection(IDataBroker dataBroker){
        
        	try {
        	
				return dataBroker.OpenConnection();
				
        	} catch (Exception) {
        		
        		throw;
        	}
        	
        }

        /// <summary>
        /// Procédure qui crée un object Principal qui va contenir les informations pour la sécurité
        /// </summary>
        /// <param name="userName">Code de l'utilisateur</param>
        /// <param name="applicationName">Nom de l'application</param>
        /// <remarks></remarks>

        public static void CreatePrincipal(string name,List<string> roles)
        {
        	
        	try
            {
            	if (SecurityManager.Instance._dataBroker.Connection.State == ConnectionState.Open) {
            		
            		Principal principal = new Principal(new Identity(name),roles);
                    Thread.CurrentPrincipal = principal;
            	}
                
            }
            catch (Exception ex)
            {
                AppException securityException = new AppException("MSG_SECURITE_ERREUR_PRINCIPAL",ex);
                throw securityException;
            }
        }

        /// <summary>
        /// Fonction qui vérifie si l'usager possède un rôle
        /// </summary>
        /// <param name="role">Nom du rôle</param>
        /// <returns>Boolean</returns>
        /// <remarks></remarks>
        public static bool IsInRole(string role)
        {
            IPrincipal principal = Thread.CurrentPrincipal;
            return principal.IsInRole(role);
        }

        /// <summary>
        /// Fonction qui permet de changer un mot de passe 
        /// </summary>
        /// <param name="userName">Code de l'utilisateur</param>
        /// <param name="oldPassword">Ancien mot de passe</param>
        /// <param name="password">Nouveau mot de passe</param>
        /// <returns>Boolean</returns>
        /// <remarks></remarks>
        public static bool ChangePassword(string userName, string oldPassword, string password)
        {
        	throw new Exception("Method not implemented.");

        }

        #endregion

    }
}